import 'package:flutter/material.dart';
import 'package:xiandou/home/RankingView.dart';
import 'package:xiandou/home/HomeView.dart';
import 'package:xiandou/home/PersonalView.dart';

class BottomNavigationWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => BottomNavigationWidgetState();
}

class BottomNavigationWidgetState extends State<BottomNavigationWidget> {
  final _bottomNavigationColor = Colors.black;
  int _currentIndex;
  List<Widget> list = List();
  PageController _controller;

  double _imageWidth = 24;
  double _imageHeight =24;
  Color selectedTitleColor = Color(0xff333333);
  Color unselectedTitleColor = Color(0xff666666);

  @override
  void initState() {
    super.initState();
    list
      ..add(HomePage())
      ..add(RankingScreen())
      ..add(PersonalView());
    _currentIndex =0;
    _controller = PageController(initialPage: 0);
  }

//  items: [
//  BottomNavigationBarItem(
//  activeIcon: getTabImage('assets/home_s.png'),
//  icon: getTabImage('assets/home.png'),
//  title: Text(
//  '任务大厅',
//  style: TextStyle(color: _currentIndex == 0 ? selectedTitleColor : unselectedTitleColor),
//  )),
//  BottomNavigationBarItem(
//  activeIcon: getTabImage('assets/ranking_s.png'),
//  icon: getTabImage('assets/ranking.png'),
//  title: Text(
//  '排行榜',
//  style: TextStyle(color: _currentIndex == 1 ? selectedTitleColor : unselectedTitleColor),
//  )),
//  BottomNavigationBarItem(
//  activeIcon: getTabImage('assets/my_s.png'),
//  icon: getTabImage('assets/my.png'),
//  title: Text(
//  '个人中心',
//  style: TextStyle(color: _currentIndex == 2 ? selectedTitleColor : unselectedTitleColor),
//  )),
//  ],

  @override
  Widget build(BuildContext context) {
    Image getTabImage(path) {
      return Image.asset(path, width: _imageWidth, height: _imageHeight);
    }

    List<BottomNavigationBarItem> getItems() {
      return [
        BottomNavigationBarItem(
            activeIcon: getTabImage('assets/home_s.png'),
            icon: getTabImage('assets/home.png'),
            title: Text(
              '任务大厅',
              style: TextStyle(color: _currentIndex == 0 ? selectedTitleColor : unselectedTitleColor),
            )),
        BottomNavigationBarItem(
            activeIcon: getTabImage('assets/ranking_s.png'),
            icon: getTabImage('assets/ranking.png'),
            title: Text(
              '排行榜',
              style: TextStyle(color: _currentIndex == 1 ? selectedTitleColor : unselectedTitleColor),
            )),
        BottomNavigationBarItem(
            activeIcon: getTabImage('assets/my_s.png'),
            icon: getTabImage('assets/my.png'),
            title: Text(
              '个人中心',
              style: TextStyle(color: _currentIndex == 2 ? selectedTitleColor : unselectedTitleColor),
            )),
      ];
    }

    void onTap(int index) {
      _controller.jumpToPage(index);
    }

    void _pageChange(int index) {
      if (index != _currentIndex) {
        setState(() {
          _currentIndex = index;
        });
      }
    }

    return Scaffold(
      body: PageView.builder(
          physics: NeverScrollableScrollPhysics(),//viewPage禁止左右滑动
          onPageChanged: _pageChange,
          controller: _controller,
          itemCount: list.length,
          itemBuilder: (context, index) => list[index]),
      bottomNavigationBar: BottomNavigationBar(
        items: getItems(),
        currentIndex: _currentIndex,
        onTap: onTap,
      ),
    );
  }
}